<?php
namespace IKPHP\Common\Models;

use IKPHP\Common\Models\BaseModel;
use IKPHP\Common\Models\User;
use IKPHP\Common\Models\VirtualModel;

class Recommend extends BaseModel
{
	public $id;
	public $typeid;
	public $type;
	public $content;
	public $userid;
	public $addtime;
	public $count_recommend;
		
    public function getSource()
    {
        return IK."recommend";
    } 
    //更新后重新计算评论
    public function afterCreate(){
    	//计算评论数
    	$count = self::count("typeid='$this->typeid'"); 
        //更新模型记录集
        $vimodel = new VirtualModel();
		$vimodel->setviSource($this->type);
		$pkey = $vimodel->getPk();
		$strMod = $vimodel->findFirst(array('conditions'=>"$pkey = $this->typeid")); 
		$strMod->count_recommend = $count;
		$this->count_recommend = $count;
		$strMod->save();  	
    } 
    //判断用户是否已经推荐过
    public static function isRecommend($uid,$typeid,$type){
    	$count = self::count("typeid='$typeid' AND type='$type' AND userid='$uid'");
    	if($count>0){
    		return true;
    	}
    	return false;
    } 
	//用户喜欢的模型数
	public static function countUserRecByType($uid, $type){
		$count = self::count("type='$type' AND userid='$uid'");
		if($count>0){
			return $count;
		}
		return 0;
	}        	   
}
